using System;
using System.Collections.Generic;
using System.Text;
using System.Data;
using System.Data.SqlClient;
using DTO;

namespace DAO
{
    public class PhieuKhamDAO
    {
        public static bool Insert(PhieuKhamDTO pk)
        {
            bool kq = false;
            try
            {
                List<SqlParameter> sqlparam = new List<SqlParameter>();
                sqlparam.Add(new SqlParameter("@MaBenhNhan", pk.MaBenhNhan));
                sqlparam.Add(new SqlParameter("@MaLoaiBenh", pk.MaLoaiBenh));
                sqlparam.Add(new SqlParameter("@MaPhieuKham", pk.MaPhieuKham));
                sqlparam.Add(new SqlParameter("@NgayKham", pk.NgayKham));
                sqlparam.Add(new SqlParameter("@TienKham", pk.TienKham));
               
                sqlparam.Add(new SqlParameter("@TrieuChung", pk.TrieuChung));
                int n = SqlDataAccessHelper.ExecuteNoneQuery("spPhieuKhamInsert",sqlparam);
                if (n == 1)
                    kq = true;
            }
            catch (Exception ex)
            {
                throw ex;
            }
            return kq;
        }
        public static bool Delete(PhieuKhamDTO pk)
        {
            bool kq = false;
            try
            {
                List<SqlParameter> sqlparam = new List<SqlParameter>();
              
                sqlparam.Add(new SqlParameter("@MaPhieuKham", pk.MaPhieuKham));
                
                int n = SqlDataAccessHelper.ExecuteNoneQuery("spPhieuKhamDelete",sqlparam);
                if (n == 1)
                    kq = true;
            }
            catch (Exception ex)
            {
                throw ex;
            }
            return kq;
        }
        public static bool Update(PhieuKhamDTO pk)
        {
            bool kq = false;
            try
            {
                List<SqlParameter> sqlparam = new List<SqlParameter>();
                sqlparam.Add(new SqlParameter("@MaBenhNhan", pk.MaBenhNhan));
                sqlparam.Add(new SqlParameter("@MaLoaiBenh", pk.MaLoaiBenh));
                sqlparam.Add(new SqlParameter("@MaPhieuKham", pk.MaPhieuKham));
                sqlparam.Add(new SqlParameter("@NgayKham", pk.NgayKham));
                sqlparam.Add(new SqlParameter("@TienKham", pk.TienKham));               
                sqlparam.Add(new SqlParameter("@TrieuChung", pk.TrieuChung));
                int n = SqlDataAccessHelper.ExecuteNoneQuery("spPhieuKhamUpdate",sqlparam);
                if (n == 1)
                    kq = true;
            }
            catch (Exception ex)
            {
                throw ex;
            }
            return kq;
        }
        public static List<PhieuKhamDTO> Select()
        {
            List<PhieuKhamDTO> lst = new List<PhieuKhamDTO>();
            try
            {
                DataTable dt = SqlDataAccessHelper.ExecuteQuery("spPhieuKhamSelect");
                foreach (DataRow dr in dt.Rows)
                {
                    PhieuKhamDTO pk = new PhieuKhamDTO();
                    pk.MaBenhNhan = int.Parse(dr["MaBenhNhan"].ToString());
                    pk.MaLoaiBenh = int.Parse(dr["MaLoaiBenh"].ToString());
                    pk.MaPhieuKham = int.Parse(dr["MaPhieuKham"].ToString());
                    pk.NgayKham = DateTime.Parse(dr["NgayKham"].ToString());
                    pk.TienKham = float.Parse(dr["TienKham"].ToString());
                   
                    pk.TrieuChung = (dr["TrieuChung"].ToString());
                    lst.Add(pk);
                }
            }
            catch (Exception ex)
            {
                throw ex;
            }
            return lst;
        }
        public static PhieuKhamDTO SelectByID(int MaPhieuKham)
        {
            PhieuKhamDTO pk = new PhieuKhamDTO();
            try
            {
                List<SqlParameter> sqlparam = new List<SqlParameter>();
                sqlparam.Add(new SqlParameter("@MaPhieuKham", MaPhieuKham));
                DataTable dt = SqlDataAccessHelper.ExecuteQuery("spPhieuKhamSelectByID", sqlparam);
                DataRow dr = dt.Rows[0];

                pk.MaBenhNhan = int.Parse(dr["MaBenhNhan"].ToString());
                pk.MaLoaiBenh = int.Parse(dr["MaLoaiBenh"].ToString());
                pk.MaPhieuKham = int.Parse(dr["MaPhieuKham"].ToString());
                pk.NgayKham = DateTime.Parse(dr["NgayKham"].ToString());
                pk.TienKham = float.Parse(dr["TienKham"].ToString());
               
                pk.TrieuChung = (dr["TrieuChung"].ToString());

            }
            catch (Exception ex)
            {
                throw ex;
            }
            return pk;
        }
        
    }
}
